Animation Brend Tree
#Unity
#Animation
#Animator
Animationに関連するComponent , Asset
https://gyazo.com/06335d4dafb3c1a8bab71a5ab399857d
Animator
Component
ゲームオブジェクトにアニメーションを割り当てる
Animator Controllerをプロパティに取る
Animator Controller
Asset (Scriptable Object)
animationを遷移させる
いわゆる finite state machine
Animation
Animation Clip とも
特定のボーンに対してキーフレームアニメーションが記録されたファイル
Brend Tree
複数のアニメーションをブレンドしてスムーズに繋げる
Animator Controller を作成
https://gyazo.com/8a43ceb5d5121699b93d585c20fb7b8a
Characterフォルダに作る(今後はここにアニメーション関連をまとめる)
Player/Enemy 共通
Create > Animator
Animator タブ
Asset Library で animatorファイルをダブルクリックするか、Windows > Animator で開く
Locomotion = 移動、歩行
Create State > BrendTree
Animatorタブのなにもないところで右クリ > Create State > From new Brend Tree
これでBrendTreeを作れる
名前をLocomotionに変えておく(Inspecter)
Edit BrendTree
ダブルクリックすると内部に入って変更できる。
1.Animator Tab > BrendTreeをクリック
2. Inspector > Add Motion Field
https://gyazo.com/89ad842c3a5e523ecdc5752f6edb3bea
3. Idle > Walk > Run の順で追加する
4. Threshold などが自動で定義される
Blend値によって切り替わる値
https://gyazo.com/69b4f8a14d6a6ca41bfbd33ed98b3e69
5. Preview でモーションブレンドのサンプルを再生できる(変なおっさんがモデル)
6. Animator Tab > BrendTree の Blend 値をいじると、Previewのモーションも変化する。
https://gyazo.com/a42fdbf7de4ecdca8421b14e4e4766f1
待機 > 歩き > 走りのように、
有機的にアニメーションが変化することでリアル感が出る
Match Animation to Move
値によってアニメーションをミックスするBlend値は別の名前にできる
ここでは、fowardSpeedに変更する
https://gyazo.com/a035556ff2ad7c7de1144fb08f5d58a3
Velocity
https://gyazo.com/a9e3dddcc1d5e4bcb9f540592dbb242d
Blend Value に fowardSpeed という名前をつけたので、
キャラクターの前進速度とアニメーションブレンドを連携させる。
キャラクターTransformのローカル軸だと、Z 軸が前進方向
やることは、
1. NavMeshAgentのグローバル速度を取得
2. キャラクターに関連したローカル速度に変換する
3. Animatorの Blend値を z軸の速度にセットする
Thresholdの調整
現在、AnimatorにはRunWalkIdleがあり、
Blendのしきい値は1 , 0.5 , 0 になっている。
実際には、歩く速度が 1 として 走る速度が 5 くらいあるので
このしきい値は正しくない
(ついでに、Blend値は 0 ~ 1 という決まりもない)
Animation Clip の移動量確認
https://gyazo.com/504a207fe46122283fd70cc4ef484a32 https://gyazo.com/c5d6bf2e45169424ec26d1a7c7c7ffcd
Animation Clipのインポートはこの様になっている
ImportSetting
Animation (三角マーク)
Hips (Transform)
DemoAvatar (変なおっさん)
アニメーションを選んで、インスペクターの下の方にモーションの平均移動速度が書いてある。
それを参考にThreshorldを変更する。
といっても、BlendTreeを選択して
Automate Thresholdのチェックを外し
Computed Threshold > Z Axisを選択すれば上の量でブレンド値が自動で設定される。
https://gyazo.com/5d46435695bb2f092f584582fb50c610
NavMeshAgentの速度をAnimatitor paramに反映
Animator > Blendtree の Base Layer のプロパティを forwardSpeedに変更
code: .fs
type Mover() =
inherit MonoBehaviour()
...
member this.Update() =
if Input.GetMouseButton(0) then
do this.lastLay <- Camera.main.ScreenPointToRay(Input.mousePosition)
this.MoveToCursor()
Debug.DrawRay(this.lastLay.origin, (float32 (100) * this.lastLay.direction))
this.UpdateAnimator()
member this.UpdateAnimator() =
this.GetComponent<NavMeshAgent>().velocity
|> this.transform.InverseTransformDirection
|> (fun localVelocity -> this.GetComponent<Animator>().SetFloat("forwardSpeed", localVelocity.z))
UpdateAnimator()メソッドを定義して、Updateで毎フレーム呼ぶ
コレにより、Aniamtion Blend の値を更新する。animator.SetFlowat("fowardSpeed", float)
Angular Speed & Acceleration
全身アニメーションしか実装していないと、方向の変わり目がぎこちない
AngularSpeed = 5000
Acceleration = 50
くらいにすると快適に操作できる
(Walkアニメが殆ど見えないが)
Input. GetMousebutton vs GetMouseButtonDown
GetMouseButton : 押している間ずっとtrue
GetMouseButtonDown : 押した瞬間だけ
RayCastは画面でマウスを押し続けている時は連続で照射するようにする。